<template>
  <div class="my_warp app_content">
    <!-- 导航栏 -->
    <div class="header_body">
      <header-nav :leftArrow="true" titelText="个人信息"></header-nav>
    </div>
    
    <van-form :label-width="130" @submit="onSubmit">
        <van-field name="avator" label="头像">
  				<template #input>
            <van-uploader v-if="!fileList.length" :deletable="false" :max-count="1" v-model="fileList" :after-read="afterRead" />
            <img v-if="fileList.length" style="width: 50px;height: 50px;" :src="fileList[0].url"/>
            <div style="margin-left: 6px;color: red;font-size: 14px;" v-if="fileList.length" @click="deleteImg">替换</div>
					</template>
				</van-field>
        <van-field
          v-model="form.username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="form.password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-model="form.name"
          name="name"
          label="姓名"
          placeholder="姓名"
          :rules="[{ required: true, message: '请填写姓名' }]"
        />
				<van-field name="sex" label="性别">
  				<template #input>
						<van-radio-group
							:rules="[{ required: true, message: '请选择性别' }]"
							v-model="form.sex" direction="horizontal">
							<van-radio name="男">男</van-radio>
							<van-radio name="女">女</van-radio>
						</van-radio-group>
					</template>
				</van-field>
        <van-field
          v-model="form.age"
          name="age"
          label="年龄"
          placeholder="年龄"
          :rules="[{ required: true, message: '请填写年龄' }]"
        />
        <van-field
          v-model="form.height"
          name="height"
          label="身高(米)"
          placeholder="身高"
          :rules="[{ required: true, message: '请填写身高' }]"
        />
        <van-field
          v-model="form.targetweight"
          name="targetweight"
          label="目标体重(公斤)"
          placeholder="目标体重"
          :rules="[{ required: true, message: '请填写目标体重' }]"
        />
        <van-field name="taste" label="口味">
  				<template #input>
            <van-dropdown-menu>
              <van-dropdown-item v-model="form.taste" :options="tastes" />
            </van-dropdown-menu>
					</template>
				</van-field>
        <van-field
          v-model="form.sign"
          name="sign"
          label="个性签名"
          placeholder="个性签名"
          :rules="[{ required: true, message: '请填写个性签名' }]"
        />
        <div>
          <van-button block type="primary" native-type="submit"
            >修改</van-button
          >
        </div>
      </van-form>
    <!-- 底部 -->
    <div class="footer_body">
      <footer-nav></footer-nav>
    </div>
  </div>
</template>

<script setup name="Info">
import { ref, reactive } from 'vue';
import { showDialog } from 'vant';
import swiperImage from '@/assets/images/defaultUser.jpg';
const router = useRouter();
import { upload, updateUser } from "@/api/http.js";
let form = reactive({
	username: '',
	password: '',
	name: '',
	sex: '男',
	age: '',
	height: '',
  taste: '请选择'
})
let tastes = ref([
  {
    text: '请选择',
    value: '请选择'
  },
  {
    text: '重口味',
    value: '重口味'
  },
  {
    text: '清淡',
    value: '清淡'
  },
  {
    text: '甜食爱好者',
    value: '甜食爱好者'
  },
  {
    text: '酸味偏好者',
    value: '酸味偏好者'
  },
  {
    text: '辣味爱好者',
    value: '辣味爱好者'
  }
])
let fileList = ref([]);
const onSubmit = async (values) => {
  console.log(form)
  if (form.taste == '请选择') {
    showDialog({
      title: '提醒',
      message: '请选择口味',
    }).then(() => {
      // on close
      console.log(111)
    });
    return
  }
  updateUser({
		...form,
	}).then((res) => {
    if (res.code == "200") {
      showDialog({
        title: '提醒',
        message: '个人信息已更新',
      }).then(() => {
        localStorage.setItem('appUserInfo', JSON.stringify(res.data));
        router.push("/my");
      });
    } else {
      showDialog({
        title: '提醒',
        message: res.message,
      }).then(() => {
      });
    }
  });
};

const deleteImg = () => {
  fileList.value.splice(0, 1)
}

const afterRead = (file) => {
  let formData = new FormData();
  formData.append('file', file.file)
  upload(formData).then(res => {
    form.avator = res.url
    fileList.value.splice(0, 1, {
      url: res.url,
      isImage: true
    });
    console.log(fileList.length)
  })
}

onMounted(() => {
	if (localStorage.getItem('appUserInfo')) {
    let xx = JSON.parse(localStorage.getItem('appUserInfo'))
    if (!xx.taste) {
      xx.taste = '请选择'
    }
    Object.assign(form, xx);
  }
});

</script>
<style lang="less" scoped>
.flex-center {
  display: flex;
  align-items: center;
}
.app_content {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background-color: #f4f4f4;
}

.weights {
  width: 90%;
  height: 60px;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  font-size: 16px;

  .each {
    display: flex;
    flex: 1;
    flex-direction: column;

    div {
      width: 100%;
      text-align: center;
    }
  }
}
.menus {
  width: 82%;
  padding: 20px;
  display: flex;
  flex-direction: column;
  font-size: 14px;

  .each-menu {
    width: 100%;
    height: 50px;
    background: #fff;
    border-radius: 10px;
    padding: 0 20px;
    margin-bottom: 12px;
  }
}
.fs22 {
  font-size: 22px;
  margin-right: 4px;
}
.User {
  width: 100%;
  height: 140px;
  background:-webkit-linear-gradient(top, #ff8198,#f585cb,);
  margin-bottom: 10px;
}
.userImg {
  display: flex;
  align-items: center;
  margin-left: 20px;
  padding-top: 30px;
  padding-bottom: 10px;
  img {
    border-radius: 100%;
    margin-right: 10px;
  }
  span {
    color: white;
    font-weight: 600;
  }
  .sign {
    font-size: 14px;
    color: #333;
    margin-top: 4px;
  }
}
.user-collection {
  ul {
    display: flex;
    align-items: center;
    li {
      display: flex;
      align-items: center;
      flex-direction: column;
      font-size: 14px;
      flex: 1;
      color: white;
    }
  }
}
</style>
